﻿
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Web.Mvc;

namespace CodeSaude.Infrastructure.CrossCutting.Util
{
    public static class MvcUtil
    {
        #region RENDER PARTIAL TO STRING

        public static string RenderPartialToString(string viewName, object model, ControllerContext controllerContext, string partialFieldName)
        {
            if (string.IsNullOrEmpty(viewName))
                viewName = controllerContext.RouteData.GetRequiredString("action");
            ViewDataDictionary ViewData = new ViewDataDictionary();
            TempDataDictionary TempData = new TempDataDictionary();
            ViewData.Model = model;
            ViewData.TemplateInfo.HtmlFieldPrefix = partialFieldName;

            using (StringWriter sw = new StringWriter())
            {
                ViewEngineResult viewResult = ViewEngines.Engines.FindPartialView(controllerContext, viewName);
                ViewContext viewContext = new ViewContext(controllerContext, viewResult.View, ViewData, TempData, sw);
                viewResult.View.Render(viewContext, sw);

                return sw.GetStringBuilder().ToString();
            }

        }

        public static object RetrirarReferenciasAgendaClone(object newObj)
        {
            newObj.GetType().GetProperty("AgendaId").SetValue(newObj, 0, null);
            newObj.GetType().GetProperty("Agenda").SetValue(newObj, null, null);
            return newObj;
        }

        #endregion
    }
}
